package com.example.queryexpressdelivery_01.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.queryexpressdelivery_01.R;
import com.example.queryexpressdelivery_01.common.DeliveryDAL;
import com.example.queryexpressdelivery_01.entity.FeedbackData;
import com.example.queryexpressdelivery_01.entity.UserData;
import com.google.gson.Gson;

import java.util.List;

public class ReplyAdapter extends RecyclerView.Adapter<ReplyAdapter.ReplyViewHolder> {
    private List<String> questions; // 假设每个问题是一个字符串
    private Context context;
    private DeliveryDAL deliveryDAL;
    private Gson gson = new Gson();
    private int userId;
    public ReplyAdapter(List<String> questions, Context context,int userId) {
        this.questions = questions;
        this.context = context;
        this.userId = userId;
    }

    @Override
    public ReplyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_reply, parent, false);
        return new ReplyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ReplyViewHolder holder, int position) {
        String question = questions.get(position);
        holder.tvQuestion.setText(question);
        holder.btnReply.setOnClickListener(v -> showReplyDialog(position,question));
    }

    @Override
    public int getItemCount() {
        return questions.size();
    }

    public void removeQuestion(int position) {
        questions.remove(position);
        notifyItemRemoved(position);
    }

    private void showReplyDialog(int position,String question) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        deliveryDAL = new DeliveryDAL(context);
        builder.setTitle("输入回复信息");

        final EditText input = new EditText(context);
        input.setInputType(InputType.TYPE_CLASS_TEXT);
        builder.setView(input);

        builder.setPositiveButton("确定", (dialog, which) -> {
            String reply = input.getText().toString();
            UserData userData = deliveryDAL.queryById(userId);
            if (userData!=null){
                if (userData.getData()!=null){
                    FeedbackData feedbackData = gson.fromJson(userData.getData(),FeedbackData.class);
                    Log.i("TAG","得到："+feedbackData.getProblemfeedback().size());
                    for (int i=0;i<feedbackData.getProblemfeedback().size();i++){
                        Log.i("TAG","get:"+feedbackData.getProblemfeedback().get(i).getProblem());
                        Log.i("TAG",question);
                        String a = "用户反馈："+feedbackData.getProblemfeedback().get(i).getProblem();
                        if (a.equals(question)){
                            Log.i("TAG","挤奶");
                            feedbackData.getProblemfeedback().get(i).setRecover(reply);
                            break;
                        }
                    }
                    String json = gson.toJson(feedbackData);
                    userData.setData(json);
                    long ans = deliveryDAL.updateUser(userData);
                    Log.i("TAG","获取到ans:"+ans);
                    if (ans!=0) Log.i("TAG","修改成功");
                    else Log.i("TAG","修改失败");
                }
            }
//            if (questions!=null){
//                if (questions.size()>0){
//                    for (int i=0;i<questions.size();i++){
//                        if (questions.get(i).equals(question)){
//                            questions.remove(i);
//                            break;
//                        }
//                    }
//                }
//            }
            removeQuestion(position);
        });

        builder.setNegativeButton("取消", (dialog, which) -> dialog.cancel());

        builder.show();
    }
    public static class ReplyViewHolder extends RecyclerView.ViewHolder {
        TextView tvQuestion;
        Button btnReply;

        public ReplyViewHolder(View itemView) {
            super(itemView);
            tvQuestion = itemView.findViewById(R.id.tvQuestion);
            btnReply = itemView.findViewById(R.id.btnReply);
        }
    }
}

